﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SSN.Objects;
using SSN.DotNet;

using SSN.BusinessFramework.Documents;

namespace SSN.WebFramework.Controls
{
    /// <summary>
    /// Specialized link control that formats the url for getting a document by providing just an ID.
    /// </summary>
    public class DocLink : HyperLink
    {
        private const string DOC_URL = "/common/getdocument.aspx?id={0}";
        public int DocID { get; set; }
        public int DevID { get; set; }

        public DocLink()
        {
            LinkType = WebFramework.Controls.LinkType.Pdf;
            NoCache = true;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (this.Page is AuthPage)
            {
                AuthPage page = (AuthPage)this.Page;
                ParameterManager pm = page.ParameterManager;
                DocumentManager dMgr = new DocumentManager(page.SecurityManager.SecurityContext);
                Document d = dMgr.GetDocument(DocID);
                bool isDev = ConfigManager.GetConfigSetting("devmode", "true").ToBool();
                if (d != null)
                {
                    NavigateUrl = pm.EncryptUrlFormat(DOC_URL, isDev ? (DevID > 0 ? DevID : DocID) : DevID);
                }
                else
                {
                    NavigateUrl = "javascript: Null();";
                    Text = string.Format("[Invalid form id for {0}]", isDev ? "dev data" : "production data");
                }
            }

            base.Render(writer);
        }
    }
}

